package com.example.mall.web.controller;

import com.example.mall.domain.UserPoints;
import com.example.mall.dto.UserPointsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserPointsQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserPointsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userPoints")
public class UserPointsController {

    @Autowired
    public IUserPointsService userPointsService;

    /**
     * 分页查询用户积分表信息
     * @param userPointsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户积分表信息",notes = "分页查询用户积分表信息",httpMethod = "GET")
    public JSONResult queryPage(UserPointsQuery userPointsQuery){
        PageDTO<UserPointsDto> userPointsDtoPageDTO = userPointsService.queryPage(userPointsQuery);
        return JSONResult.success("查询成功",userPointsDtoPageDTO);
    }

    /**
     * 根据id查询用户积分表信息
     * @param pointId
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询用户积分表信息",notes = "根据id查询用户积分表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("pointId")Long pointId){
        //1.根据id查询用户信息
        if (pointId == null){
            throw new GlobalCustomerException(ErrorEnumCode.POINTID_NOT_ID);
        }
        UserPoints userPoints = userPointsService.getById(pointId);
        return JSONResult.success("查询成功",userPoints);
    }

    /**
     * 根据id查询用户积分表信息
     * @param pointId
     * @return
     */
    @GetMapping("queryUserPointsById/{pointId}")
    @ApiOperation(value = "根据id查询用户积分表信息",notes = "根据id查询用户积分表信息",httpMethod = "GET")
    public JSONResult queryUserPointsById(@PathVariable("pointId")Long pointId){
        //1.根据id查询用户信息
        if (pointId == null){
            throw new GlobalCustomerException(ErrorEnumCode.POINTID_NOT_ID);
        }
        UserPoints userPoints = userPointsService.queryUserPointsById(pointId);
        return JSONResult.success("查询成功",userPoints);
    }

    /**
     * 查询全部用户积分表信息
     * @param
     * @return
     */
    @GetMapping("queryUserPointsList")
    @ApiOperation(value = "查询全部用户积分表信息",notes = "查询全部用户积分表信息",httpMethod = "GET")
    public JSONResult queryUserPointsList(){
        List<UserPoints> userPoints = userPointsService.queryUserPointsList();
        return JSONResult.success("查询成功",userPoints);
    }

    /**
     * 新增用户积分表
     * @param userPointsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户积分表",notes = "新增用户积分表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserPointsDto userPointsDto){
        userPointsService.insert(userPointsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户积分表信息
     * @param userPointsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户积分表信息",notes = "修改用户积分表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserPointsDto userPointsDto){
        userPointsService.update(userPointsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户积分表信息
     * @param pointId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户积分表信息",notes = "删除用户积分表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("pointId") Long pointId) {
        userPointsService.delete(pointId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户积分表信息
     * @param pointIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户积分表信息",notes = "批量删除用户积分表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> pointIds) {
        userPointsService.deleteBatch(pointIds);
        return JSONResult.success("批量删除成功");
    }
}
